package com.petalsgroup.petals.dao.user.impl;

import com.petalsgroup.petals.dao.user.UserDao;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {
    //使用数据库连接池
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override//判断用户名是否存在
    public UserDO findUserByUsername(UserDO user) {

        Object[] object = new Object[1];
        object[0] = user.getUsername();

        //1.定义sql
        String sql = "select * from user where username = ?";
        //2.执行sql - 不会返回null,会报异常,需要解决
        List<UserDO> list = template.query(sql,object, new BeanPropertyRowMapper<UserDO>(UserDO.class));
        if (list!=null && list.size() >0){
            return list.get(0);
        }else {
            return null;
        }
    }

    @Override//注册
    public void register(UserDO user) {
        //1.定义sql - 最后两个参数一开始可以不要
        String sql = "insert into user" +
                "(username,password,banned," +
                "name,create_time,email,code,active_state) " +
                "values(?,?,?,?,?,?,?,?)";
        template.update(sql,user.getUsername(),user.getPassword(),user.getBanned(),user.getName(),
                user.getCreate_time(),user.getEmail(),user.getCode(),user.getActive_state());


    }

    @Override
    public UserDO findUuid(String code) {
        String sql = "select * from user where code = ?";
        List<UserDO> list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class), code);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public void updateStatus(String code) {
        String sql = "UPDATE user SET `active_state` = 'Y' WHERE code = ?";
        template.update(sql,code);
    }

    @Override
    public UserDO login(String username, String password) {
        //编写sql
        String sql = "select * from user where active_state='Y' and username = ?and password=? ";

        /*Object[] object = new Object[2];
        object[0]=username;
        object[1]=password;*/
        List<UserDO> list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class), username, password);
        //List<UserDO> list = template.query(sql, object, new BeanPropertyRowMapper<UserDO>(UserDO.class));
        if (list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public UserDO findUserByUsernameAndEmail(UserDO user) {
        String sql = "select * from user where username = ? and email = ?";
        List<UserDO> list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class), user.getUsername(),user.getEmail());
        //System.out.println(list.get(0).getPassword());
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public String getNameByUid(int uid) {
        String sql = "select name from user where uid =?";
        return template.queryForObject(sql, String.class, uid);
    }
    /**
     * 获得某个用户被举报的次数
     * @param uid
     * @return
     * @author wgd
     */
    @Override
    public Integer getUserInformNumByUid(Integer uid) {
        String sql="select inform_num from user where uid = ?";
        Integer num = template.queryForObject(sql, Integer.class, uid);
        return num;
    }


    /**
     * 更新用户的被举报次数
     * @param uid
     * @param inform_num
     * @author wgd
     */
    @Override
    public void updateUserInformNumByUid(Integer uid, Integer inform_num) {
        String sql="update user set inform_num = ? where uid = ?";
        template.update(sql,inform_num,uid);
    }

    @Override
    public UserDO findUserByUid(int uid) {
        String sql = "select * from user where uid = ?";
        List<UserDO> list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class), uid);
        //System.out.println(list.get(0).getPassword());
        return list.size() == 0 ? null : list.get(0);
    }
}
